package platform.nio.client;

import platform.bio.client.BioClientHandler;
import platform.common.CommonInfo;
import platform.nio.server.NioServerHandler;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;

public class NioClient {

    public static void main(String[] args) throws Exception {
        Selector selector = Selector.open();
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);

        boolean isConnect = socketChannel.connect(new InetSocketAddress(CommonInfo.url, CommonInfo.port));
        if(isConnect){
            socketChannel.register(selector, SelectionKey.OP_READ);
        }else {
            socketChannel.register(selector, SelectionKey.OP_CONNECT);
        }

        System.out.println("nio启动");
        new NioServerHandler(selector,Charset.forName("GBK")).start();

    }
}
